<template>
  <view class="content">
    <!-- <image class="logo" src="@/static/newlogo.png"></image> -->
	<uni-section title=""  padding > <!-- type="circle" -->
		<uni-countdown :font-size="30" :show-day="false" :hour="timer.hour" :minute="timer.minute" :second="timer.second" color="#FFFFFF" background-color="#007AFF" />
	</uni-section>
	<uni-notice-bar show-icon scrollable v-if="showNotice" :text="notices" />
	<view class="example-body">
		<view  class="Today-add">
			<uni-title type="h1" title="Today"></uni-title>
			<!-- icon -->
			<uni-icons type="plusempty" size="30" @click="planAdd()"></uni-icons>			
		</view>
		<uni-list v-for="(item, index) in contentList" :key="index" >
			<uni-list-item  >
				<template v-slot:header >
					<view class="slot-box" @click="itemClick(item)" >
						<text class="slot-text">{{item.title}}</text>
					</view>
				</template>
				<template v-slot:footer>
					<switch :checked="item.studyStatus=='0'?false:true" @change="switchChange($event,item,1)"/>
				</template>
			</uni-list-item>
		</uni-list>
		<uni-title type="h1" title="Review"></uni-title>		
		<uni-list v-for="(item, index) in reviewList" :key="index+'v'">
			<uni-list-item>
				<template v-slot:header >
					<view class="slot-box" @click="itemClick(item)" >
						<text class="slot-text">{{item.title}}</text>
					</view>
				</template>
				<template v-slot:footer>
					<switch :checked="item.reviewStatus=='0'?false:true" @change="switchChange($event,item,2)" />
				</template>
			</uni-list-item>
		</uni-list>
    </view>
  </view>
</template>

<script>
import { listSubject} from "@/api/plan/subject";
import { todyTodoList,todyReviewList} from "@/api/plan/workSpace";
import { updateContent,getContent,addContent} from "@/api/plan/content";
import { updateReview} from "@/api/plan/review";
import { getInfoRandom} from "@/api/plan/notice";
export default {
	data() {
		return {
			timer:{
				hour:'',
				minute:'',
				second:''
			},
			notices:'',
			showNotice:false,
			searchVal: '',
			// 学习内容表格数据
			contentList: [],
			// 复习内容表格数据
			reviewList: [],
			loading: false
		}
	},
	onLoad() {
		this.getTodyTodoList();
		this.getTodyReviewList();
		this.reflashTime();
		// 从完成页返回该页面的获取数据
		uni.$on('refreshData',(msg) => {
			this.contentList = [];
			this.reviewList = [];
			this.getTodyTodoList();
			this.getTodyReviewList();
			this.reflashTime();
		})
		
	},
	created() {		
		this.getNotice();
	},
	methods: {
		getNotice(){
			getInfoRandom().then(res=>{
				if(res.data.length > 0){
					this.showNotice = true;
					this.notices = res.data[0].notice;
				}
			});
		},
		reflashTime(){
			let now = new Date();
			this.timer.hour = 23 - now.getHours();
			this.timer.minute = 59 - now.getMinutes();
			this.timer.second = 59 - now.getSeconds();			
			// console.log("距离午夜还有 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒");
		},
		itemClick(item){
			let jsonObj = JSON.stringify(item)
			uni.navigateTo({
			  url:'/pages/plan/planDetail?item=' + jsonObj
			})
		},
		planAdd(){
			uni.navigateTo({
			  url:'/pages/plan/planAdd'
			})
		},
		switchChange(e,item,flag) {   
			item["flag"] = flag;
			if(e.detail.value === true){
				let jsonObj = JSON.stringify(item)
				uni.navigateTo({
				  url:'/pages/plan/planEditor?item=' + jsonObj
				})
			}else{
				//flag : 1  content   2  review
				if(item.flag === 1){
					item.studyStatus = '0';
					updateContent(item);
				}else{
					item.reviewStatus = '0';
					let review = {
						id:item.rid,
						reviewStatus:item.reviewStatus
					}
					updateReview(item);			
				}
			}	
					
        },		
        /** 查询学习内容列表 */
        getTodyTodoList() {			
            todyTodoList().then(response => {
                this.contentList = response.rows;
                this.loading = false;
            });
        },
        /** 查询复习内容列表 */
        getTodyReviewList() {
            todyReviewList().then(response => {
                this.reviewList = response.rows;
                this.loading = false;
            });
        },
	}
}
</script>


<style>
/*  .content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  } */
	.Today-add{
		display: flex;
	}
	.Today-add .uni-icons{		
		padding: 5px 20px 5px 5px;
	}
	.slot-box{
		width: 75%;		
	}
	.logo {
		height: 200rpx;
		width: 200rpx;
		/* margin-top: 200rpx; */
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
